local FightEffect = class("FightEffect")
local FightConst = require("const.FightConst")
local AttributeConst = require("const.AttributeConst")
local CountCondition = require("gameLogic.fight.fightSheild.CountCondition")
local AttrDataHelper = require("data.helper.AttrDataHelper")

function FightEffect:ctor(config, entity)
    self._config = config
    self._entity = entity
end

function FightEffect:getEntity()
    return self._entity
end

function FightEffect:getTrigger()
    return self._config.trigger
end

function FightEffect:getEffectId()
    return self._config.id
end

function FightEffect:isDelTriggerAfter()
    return self._config.delete == 1
end

function FightEffect:getEffectName()
    return self._config.name
end

------------------------- 效果触发条件 --------------------------
----------------------------------------------------------------

function FightEffect:effectPlay(param, battleReport, manager)
    if self._entity and self._entity:isDie() then
        return
    end
    self._manager = manager
    self._battleReport = battleReport
    if self._config.trigger == FightConst.EFFECT_TRIGGER_ROUND and param.round ~= self._config.tParam then
        return
    end
    local effectFunc = self["_" .. self._config.class .. "_effect"]
    if effectFunc then
        return effectFunc(self, param)
    end
end

function FightEffect:_addFakeAttr(attrId, addValue, entity)
    local curValue = entity:getFakeAttrValue(attrId)
    local newValue = curValue + addValue
    entity:setFakeAttrValue(attrId, newValue)
    return true
end

-------------------- 特性函数 -------------------------------
function FightEffect:_hurtRace_effect(param)
    local race = tonumber(self._config.param2)
    local attrId = AttrDataHelper.getRaceHurtAddAttrId(race)
    self:_addFakeAttr(attrId, self._config.param, self._entity)
    self._battleReport:addFightEffect(self, self._entity)
    return true
end

function FightEffect:_roundHurt_effect(param)
    local ratio = self._config.param / 1000
    local curValue = self._entity:getBaseAttrValue(AttributeConst.ATTACK)
    local addValue = math.floor(curValue * ratio)
    self:_addFakeAttr(AttributeConst.ATTACK, addValue, self._entity)
    self._battleReport:addFightEffect(self, self._entity)
    return true
end

function FightEffect:_sexHurt_effect(param)
    local sex = tonumber(self._config.param2)
    local attrId = AttrDataHelper.getSexHurtAttrId(sex)
    self:_addFakeAttr(attrId, self._config.param, self._entity)
    self._battleReport:addFightEffect(self, self._entity)
    return true
end

function FightEffect:_yumeiDeBuff_effect(param)
    local enemys = self._entity:getEnemys()
    return true
end

function FightEffect:_raceBuff_effect(param)
    local race = tonumber(self._config.param2)
    local friends = self._entity:getFriends()
    local count = 0
    for i = 1, #friends do
        local feature = friends[i]:getFeature()
        if feature.race == race then
            count = count + 1
        end
    end
    local addValue = self._config.param * count
    for i = 1, #friends do
        local feature = friends[i]:getFeature()
        if feature.race == race then
            self:_addFakeAttr(AttrDataHelper.getRaceHurtAddAttrId(race), addValue, friends[i])
            self._battleReport:addFightEffect(self, friends[i])
        end
    end
    return true
end

function FightEffect:_sheildBroken_effect(param)
    local entityId = param.entityId
    local excuter = self._entity:getExcuter()
    local entity = excuter:getEntityById(entityId)
    if self._entity:getId() ~= entityId and self._entity:getCamp() == entity:getCamp() then
        local norSkill = self._entity:getNormalSKill()
        local enemys = self._entity:getEnemys()
        local index = math.random(1, #enemys)
        local enemy = enemys[index]
        local hurts = norSkill:pureAct({enemy}, battleReport)
        self._battleReport:addHelpAttack(hurts[1], self._entity, enemy, norSkill:getId())
        self._battleReport:addFightEffect(self, self._entity)
        return true
    end
end

function FightEffect:_helpAttack_effect(param)
    local entityId = param.entityId
    local excuter = self._entity:getExcuter()
    local entity = excuter:getEntityById(entityId)
    if entityId ~= self._entity:getId() and entity:getCamp() == self._entity:getCamp() then
        local roundCount = self._manager:getExcuter():getRoundCount()
        if not self._lastRoundCount then
            self._lastRoundCount = 0
        end
        local callAttack = function()
            local norSkill = self._entity:getNormalSKill()
            local enemys = self._entity:getEnemys()
            local index = math.random(1, #enemys)
            local enemy = enemys[index]
            local hurts = norSkill:pureAct({enemy}, battleReport)
            self._battleReport:addHelpAttack(hurts[1], self._entity, enemy, norSkill:getId())
            self._battleReport:addFightEffect(self, self._entity)
        end
        if self._lastRoundCount ~= roundCount then
            self._lastRoundCount = roundCount
            callAttack()
            self._count = 1
            return true
        else
            if self._count < self._config.param then
                callAttack()
                return true
            end
            self._count = self._count + 1
        end
    end
end

function FightEffect:_piety_effect(param)
    self:_addFakeAttr(AttributeConst.CONFUSION_RESIST, self._config.param, self._entity)
    return true
end

function FightEffect:_shaChang_effect(param)
    local friends = self._entity:getFriends()
    local sex = tonumber(self._config.param2)
    local count = 0
    for i = 1, #friends do
        local feature = friends[i]:getFeature()
        if feature.sex == sex then
            count = count + 1
        end
    end
    local addValue = self._config.param * count
    for i = 1, #friends do
        self:_addFakeAttr(AttributeConst.HURT_ADD, addValue, friends[i])
        self._battleReport:addFightEffect(self, friends[i])
    end
    return true
end

function FightEffect:_justAttack_effect(param)
    local ratio2 = tonumber(self._config.param2) / 1000
    local defValue = self._entity:getBaseAttrValue(AttributeConst.DEFENSE)
    local addValue = -defValue * ratio2
    self:_addFakeAttr(AttributeConst.DEFENSE, addValue, self._entity)
    self:_addFakeAttr(AttributeConst.HURT_ADD, self._config.param, self._entity)
    self._battleReport:addFightEffect(self, self._entity)
    return true
end

function FightEffect:_poShi_effect(param)
    local enemys = self._entity:getEnemys()
    local ratio = self._config.param / 1000
    for i = 1, #enemys do
        local defValue = self._entity:getBaseAttrValue(AttributeConst.DEFENSE)
        local addValue = -defValue * ratio
        self:_addFakeAttr(AttributeConst.DEFENSE, addValue, enemys[i])
        self._battleReport:addFightEffect(self, self._entity)
    end
    return true
end

function FightEffect:_dongHe_effect(param)
    local enemys = self._entity:getEnemys()
    for i = 1, #enemys do
        self._battleReport:addFightEffect(self, enemys[i])
    end
    return true
end

function FightEffect:_knowledge_effect(param)
    self:_addFakeAttr(AttributeConst.HURT_ADD_SPELL, self._config.param, self._entity)
    self._battleReport:addFightEffect(self, self._entity)
    return true
end

function FightEffect:_fireHurt_effect(param)
    self:_addFakeAttr(AttributeConst.FIRE_HURT, self._config.param, self._entity)
    self._battleReport:addFightEffect(self, self._entity)
    return true
end

function FightEffect:_jianDun_effect(param)
    if self._entity:getId() == param.entityId then
        local ratio = tonumber(self._config.ratio2) / 1000
        local maxBlood = self._entity:getAttrValue(AttributeConst.INIT_BLOOD)
        local blood = self._entity:getAttrValue(AttributeConst.BLOOD)
        local condiValue = blood / maxBlood
        if condiValue <= ratio then
            local sheildValue = self._config.param
            local buffManager = self._manager:getExcuter():getBuffManager()
            local id = buffManager:getSheildId()
            local sheild = require("gameLogic.fight.Sheild").new(id, 1, self._config.param, 0)
            self._entity:addSheild(sheild, self._battleReport)
            return true
        end
    end
end

function FightEffect:_mianshangDun_effect(param)
    if self._entity:getId() == param.entityId then
        local blood = self._entity:getAttrValue(AttributeConst.BLOOD)
        if blood <= 0 then
            self._entity:noDie()
            local buffManager = self._manager:getExcuter():getBuffManager()
            local id = buffManager:getSheildId()
            local sheild = require("gameLogic.fight.fightSheild.NoInjurySheild").new(id, 1, 1, 0)
            sheild:setBackBloodParam(self._config.param)
            local countParam = tonumber(self._config.param2)
            local condition =
                CountCondition.new(
                SignalConst.EVENT_ROUND_BEGIN,
                function(count, round, battle)
                    if count >= countParam then
                        self._entity:removeSheild(id, battle)
                        return true
                    end
                    return false
                end
            )
            sheild:setCondition(condition)
            self._entity:addSheild(sheild, self._battleReport)
            return true
        end
    end
end

function FightEffect:_shouGe_effect(param)
    if self._entity:getId() == param.entityId then
        local excuter = self._entity:getExcuter()
        local entity = excuter:getEntityById(param.beAttackId)
        local ratio = self._config.param / 1000
        local maxBlood = entity:getAttrValue(AttributeConst.INIT_BLOOD)
        local blood = entity:getAttrValue(AttributeConst.BLOOD)
        local condiValue = blood / maxBlood
        if condiValue <= ratio then
            entity:setDie(true)
            self._battleReport:addEntityDie(true)
            self._battleReport:addFightEffect(self, self._entity, entity)
            return true
        end
    end
end

function FightEffect:_zhengyiDun_effect(param)
    local buffManager = self._manager:getExcuter():getBuffManager()
    local id = buffManager:getSheildId()
    local sheild = require("gameLogic.fight.fightSheild.BloodBackSheild").new(id, 1, self._config.param, 0)
    sheild:setBackBloodParam(tonumber(self._config.param2))
    self._entity:addSheild(sheild, self._battleReport)
    self._battleReport:addFightEffect(self, self._entity)
    return true
end

function FightEffect:_shareHurt_effect(param)
    local entityId = param.entityId
    if self._entity:getId() == entityId then
        local Hurt = require("gameLogic.fight.fightHurtStrategy.Hurt")
        local sameEntitys = self._manager:getSameEffectEntity()
        local count = #sameEntitys
        local srcHurt = param.hurt
        local signalValue = math.floor(srcHurt.value / count)
        srcHurt.value = srcHurt.value - signalValue * (count - 1)
        local dstHurt = Hurt.new(signalValue, false, srcHurt.element)
        for i = 1, #sameEntitys do
            if sameEntitys[i]:getId() ~= entitId then
                sameEntitys[i]:simpleHurt(dstHurt, self._battleReport)
                self._battleReport:addSubHurt(dstHurt, self._entity, sameEntitys[i])
                self._battleReport:addFightEffect(self, sameEntitys[i])
            end
        end
        self._battleReport:addFightEffect(self, self._entity)
    end
end
------------------------------------------------------

return FightEffect
